package com.acme.domain.account;

import com.acme.exceptions.NoEnoughFundsException;

import java.math.BigDecimal;

public class SavingAccount extends AbstractAccount {
    public SavingAccount(final int id, final BigDecimal amount) {
        super(id, amount);
    }

    @Override
    public void deposit(final BigDecimal amount) throws IllegalArgumentException {
        checkAmountIsPositive(amount);
        setBalance(getBalance().add(amount));
    }

    @Override
    public void withdraw(final BigDecimal amount) throws NoEnoughFundsException {
        checkAmountIsPositive(amount);
        if (getBalance().compareTo(amount) >= 0) {
            setBalance(getBalance().subtract(amount));
        } else {
            throw new NoEnoughFundsException(amount);
        }

    }

    @Override
    public AccountType getAccountType() {
        return AccountType.SAVING;
    }

    @Override
    public String toString() {
        return String.format("Saving account with amount: %s$", getBalance());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        SavingAccount that = (SavingAccount) o;

        if (getId() != that.getId()) return false;
        if (getBalance() != null ? !getBalance().equals(that.getBalance()) : that.getBalance() != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = getId();
        result = 31 * result + (getBalance() != null ? getBalance().hashCode() : 0);
        return result;
    }
}